package org.paybiz.core.payplatform.tenPay.service;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.paybiz.constant.PayConstant;
import org.paybiz.constant.ErrorConstant;
import org.paybiz.core.payplatform.tenPay.constant.TenPayConstant;
import org.paybiz.core.payplatform.tenPay.util.PayResponseHandler;
import org.paybiz.core.service.callback.AbstractPayCallback;
import org.paybiz.model.PayResultDO;
import org.paybiz.util.DynamicConfig;
import org.springframework.stereotype.Service;

/**
 * 支付回调
 * @author nassir.wen
 * @date 2013-1-8上午9:48:58
 * @company: MSD. 
 * @copyright: Copyright (c) 2013
 */
@Service("tenPayCallbackService")
public class TenPayCallbackServiceImpl extends AbstractPayCallback {

    private static final Log logger = LogFactory.getLog(TenPayCallbackServiceImpl.class);
    
    /**
     * @author nassir.wen
     * @date 2013-1-10下午3:05:08
     * @see org.paybiz.core.service.callback.msd.payCenter.msdPay.service.AbstractPayCallback#verifyResult(javax.servlet.http.HttpServletRequest)
     * @param request
     * @return
     */
    @Override
    protected PayResultDO verifyResult(HttpServletRequest request,HttpServletResponse response) {
        if (logger.isInfoEnabled()) {
            logger.info("begin...");
        }
        try {
            request.setCharacterEncoding("gb2312");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        response.setCharacterEncoding("gb2312");
        //密钥
        String key = DynamicConfig.getInstance().getValue(TenPayConstant.TEN_KEYVALUE);
        //创建PayResponseHandler实例
        PayResponseHandler resHandler = new PayResponseHandler(request, response);
        String transaction_id = resHandler.getParameter("transaction_id");
        String sp_billno = resHandler.getParameter("sp_billno");
        String returnUrl = resHandler.getParameter("attach");
        int total_fee = Integer.valueOf(resHandler.getParameter("total_fee"));
        String pay_result = resHandler.getParameter("pay_result");
        resHandler.setKey(key);
        Integer result = 1; //1为失败0为成功
        String errMsg = "";
        String order_id = sp_billno;

        //判断签名
        try {
            if (resHandler.isTenpaySign()) {
                //交易成功
                if ("0".equals(pay_result)) {
                    logger.info("订单号:" + order_id + "财付通支付成功！");
                    result = 0;
                } else {
                    logger.info("订单号:" + order_id + "财付通支付未成功！");
                }
            } else {
                logger.info("财付通支付交易签名被篡改！");
                errMsg = ErrorConstant.SIGN_FAIL_ID;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        PayResultDO payResultDO = new PayResultDO();
        payResultDO.setPayMoney(total_fee);
        payResultDO.setOrderId(sp_billno);
        payResultDO.setPaySerialId(transaction_id);
        payResultDO.setResult(result);
        payResultDO.setPayType(PayConstant.TENPAY);
        payResultDO.setErrMsg(errMsg);
        payResultDO.setResultUrl(returnUrl);

        return payResultDO;
    }


    /**
     * @author nassir.wen
     * @date 2013-1-10下午3:05:08
     * @see org.paybiz.core.service.callback.msd.payCenter.msdPay.service.AbstractPayCallback#send(int)
     * @param result
     * @return
     */
    @Override
    protected String send(int result) {
        //财付通不需要异步通知返回
        return null;
    }

}
